package com.mrroman.linksender.gui.actions;

import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.ResourceBundle;

import javax.swing.AbstractAction;

import com.mrroman.linksender.Configuration;
import com.mrroman.linksender.ioc.In;
import com.mrroman.linksender.ioc.Init;
import com.mrroman.linksender.ioc.Locales;
import com.mrroman.linksender.ioc.Name;
import com.mrroman.linksender.sender.Message;
import com.mrroman.linksender.sender.MessagePoint;

@Name("actions.SendClipboardAction")
public class SendClipboardAction extends AbstractAction {

	@Locales
	private ResourceBundle messages;
	
	@In
	private MessagePoint msgPoint;
	
	@In
	private Configuration config;
	
	@Init
	public void init() {
		putValue(NAME, messages.getString("send_clipboard"));
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		Transferable transferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
		
		if (transferable == null)
			return;
		
		try {
			String msg = (String)transferable.getTransferData(DataFlavor.stringFlavor);
			
			msgPoint.sendMessage(new Message(config.getNick(), msg));
		} catch (UnsupportedFlavorException e1) {
			return;
		} catch (IOException e1) {
			return;
		}
	}
	
}
